package com.senlingspace.system.service.impl;

import java.util.List;

import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.SecurityUtils;
import com.senlingspace.system.domain.SlkjEquipment;
import com.senlingspace.system.domain.SlkjEquipmentLog;
import com.senlingspace.system.domain.dto.EquipmentShopDto;
import com.senlingspace.system.domain.vo.SlkjEquipmentShopVo;
import com.senlingspace.system.mapper.SlkjEquipmentLogMapper;
import com.senlingspace.system.mapper.SlkjEquipmentMapper;
import com.senlingspace.system.mapper.SlkjShopMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjEquipmentShopMapper;
import com.senlingspace.system.domain.SlkjEquipmentShop;
import com.senlingspace.system.service.ISlkjEquipmentShopService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 门店设备Service业务层处理
 *
 * @author SenlingSpace
 * @date 2024-09-26
 */
@Service
public class SlkjEquipmentShopServiceImpl implements ISlkjEquipmentShopService {
    private static final Logger log = LoggerFactory.getLogger(SlkjEquipmentShopServiceImpl.class);
    @Autowired
    private SlkjEquipmentShopMapper slkjEquipmentShopMapper;
    @Autowired
    private SlkjEquipmentMapper slkjEquipmentMapper;
    @Autowired
    private SlkjShopMapper slkjShopMapper;
    @Autowired
    private SlkjEquipmentLogMapper slkjEquipmentLogMapper;

    /**
     * 查询门店设备
     *
     * @param id 门店设备主键
     * @return 门店设备
     */
    @Override
    public SlkjEquipmentShop selectSlkjEquipmentShopById(Long id) {
        return slkjEquipmentShopMapper.selectSlkjEquipmentShopById(id);
    }


    /**
     * 新增门店设备
     *
     * @param slkjEquipmentShop 门店设备
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class )
    public int insertSlkjEquipmentShop(SlkjEquipmentShop slkjEquipmentShop) {
        int result = 0;
        if(slkjEquipmentShop.getNum() == 0 || slkjEquipmentShop.getNum() < 0){
            throw new IllegalArgumentException("库存数量必须大于0");
        }
        //添加门店设备信息需要判断设备在门店是否有库存，如果有则需要修改变更记录
        int countSum = slkjEquipmentShopMapper.selectShopEquipment(slkjEquipmentShop.getEquipmentId(),slkjEquipmentShop.getShopId());
        if(countSum > 0 ){
            throw new IllegalArgumentException("当前门店已存在该设备，请前去修改");
        }
        slkjEquipmentShop.setCreateBy(SecurityUtils.getUsername());
        slkjEquipmentShop.setCreateTime(DateUtils.getNowDate());
        int insertEquipmentShopSum = slkjEquipmentShopMapper.insertSlkjEquipmentShop(slkjEquipmentShop);
        if(insertEquipmentShopSum > 0){
            result = slkjEquipmentMapper.updateEquipmentSum(slkjEquipmentShop.getNum(),slkjEquipmentShop.getEquipmentId());
        }else {
            throw new IllegalArgumentException("门店已设备库存添加失败");
        }
        return  result;

    }

    /**
     * 修改门店设备
     *
     * @param slkjEquipmentShop 门店设备
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSlkjEquipmentShop(SlkjEquipmentShop slkjEquipmentShop){
        if(slkjEquipmentShop.getNum() < 0 || slkjEquipmentShop.getNum() == 0){
            throw new IllegalArgumentException("库存数量必须大于0");
        }
        SlkjEquipment equipment = slkjEquipmentMapper.selectSlkjEquipmentById(slkjEquipmentShop.getEquipmentId());
        SlkjEquipmentShop equipmentShop = slkjEquipmentShopMapper.selectSlkjEquipmentShopById(slkjEquipmentShop.getId());
        // SlkjEquipmentLog equipmentLog = slkjEquipmentLogMapper.selectEquipmentShopLog(slkjEquipmentShop.getId(),slkjEquipmentShop.getShopId(),slkjEquipmentShop.getEquipmentId());
        SlkjEquipmentLog slkjEquipmentLog = new SlkjEquipmentLog();
        slkjEquipmentLog.setEquipmentId(equipmentShop.getEquipmentId());

        if(equipmentShop.getShopId() != null){
            slkjEquipmentLog.setShopId(equipmentShop.getShopId());
        }else {
            slkjEquipmentLog.setEquipmentShopId(equipmentShop.getId());
        }
        slkjEquipmentLog.setBeforeChange(equipmentShop.getNum());
        slkjEquipmentLog.setAfterChange(slkjEquipmentShop.getNum());
        slkjEquipmentLog.setUpdateTime(DateUtils.getNowDate());
        slkjEquipmentLog.setUpdateBy(SecurityUtils.getUsername());
        slkjEquipmentLogMapper.insertSlkjEquipmentLog(slkjEquipmentLog);
        slkjEquipmentShop.setUpdateTime(DateUtils.getNowDate());
        slkjEquipmentShop.setUpdateBy(SecurityUtils.getUsername());
        slkjEquipmentShopMapper.updateSlkjEquipmentShop(slkjEquipmentShop);
        equipment.setUpdateBy(SecurityUtils.getUsername());
        equipment.setUpdateTime(DateUtils.getNowDate());
        equipment.setNum(equipment.getNum() - equipmentShop.getNum() + slkjEquipmentShop.getNum());
        return slkjEquipmentMapper.updateSlkjEquipment(equipment);
    }

    /**
     * 批量删除门店设备
     *
     * @param ids 需要删除的门店设备主键
     * @return 结果
     */
    @Override
    public int deleteSlkjEquipmentShopByIds(Long[] ids) {
        return slkjEquipmentShopMapper.deleteSlkjEquipmentShopByIds(ids);
    }

    /**
     * 删除门店设备信息
     *
     * @param id 门店设备主键
     * @return 结果
     */
    @Override
    public int deleteSlkjEquipmentShopById(Long id) {
        return slkjEquipmentShopMapper.deleteSlkjEquipmentShopById(id);
    }

    @Override
    public List<SlkjEquipment> selectSlkjEquipment() {
        List<SlkjEquipment> slkjEquipments = slkjEquipmentMapper.selectSlkjEquipment();
        return slkjEquipments;
    }

    @Override
    public List<SlkjEquipmentShopVo> selectEquipmentShopList(EquipmentShopDto equipmentShopDto) {

            return slkjEquipmentShopMapper.selectEquipmentShopList(equipmentShopDto);

    }
}
